﻿using System;
using System.Linq.Expressions;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using FluentDml.Tests.TestClasses;
using NHibernate.Linq;
using NUnit.Framework;

namespace FluentDml.Tests
{
    [TestFixture]
    public class UpdateTests
    {
        private static InMemoryDatabase _db;

        [TestFixtureSetUp]
        public void FixtureSetup()
        {
            _db = new InMemoryDatabase();
        }

        [TestFixtureTearDown]
        public void FixtureTearDown()
        {
            _db.Dispose();
        }

        [SetUp]
        public void Setup()
        {
            _db.CreateSession();

            IList<Person> persons = TestData.GenerateSession<Person>().List<Person>(100).Get();

            foreach (var person in persons)
            {
                _db.Session.Save(person);
            }
    
            _db.Session.Flush();
        }

        [TearDown]
        public void TearDown()
        {
            _db.TearDownSession();
        }


        [Test]
        public void Update_Set_Age_Where_Name_Equals_James()
        {
            int count = _db.Session.Query<Person>().Where(p => p.FirstName == "James").Count();
            int count2 = _db.Session.Query<Person>().Where(p => p.FirstName == "James" && p.Age == 123).Count();

            string generateCommand = _db.Session.Update<Person>(x => x.FirstName == "James")
                .Set(x => x.Age, 123)
                .GenerateCommand();

            int executeCommand = _db.Session.Update<Person>(x => x.FirstName == "James")
                .Set(x => x.Age, 123)
                .ExecuteCommand();

            int count3 = _db.Session.Query<Person>().Where(p => p.FirstName == "James" && p.Age == 123).Count();

            Assert.AreEqual(count, count3);
        }

        [Test]
        public void Update_Set_Age_Where_Name_Contains_ame()
        {
            Expression<Func<Person, bool>> where = p => p.FirstName.Contains("ame");
            Expression<Func<Person, bool>> endWere = p => p.FirstName.Contains("ame") && p.Age == 123;

            int count = _db.Session.Query<Person>().Where(where).Count();
            int count2 = _db.Session.Query<Person>().Where(endWere).Count();

            string generateCommand = _db.Session.Update<Person>(where)
                .Set(x => x.Age, 123)
                .GenerateCommand();

            int executeCommand = _db.Session.Update<Person>(where)
                .Set(x => x.Age, 123)
                .ExecuteCommand();

            int count3 = _db.Session.Query<Person>().Where(endWere).Count();

            Assert.AreEqual(count, count3);
        }

        [Test]
        public void Update_Set_Age_Where_Name_StartsWith_Jam()
        {
            Expression<Func<Person, bool>> where = p => p.FirstName.StartsWith("Jam");
            Expression<Func<Person, bool>> endWere = p => p.FirstName.Contains("Jam") && p.Age == 123;

            int count = _db.Session.Query<Person>().Where(where).Count();
            int count2 = _db.Session.Query<Person>().Where(endWere).Count();

            string generateCommand = _db.Session.Update<Person>(where)
                .Set(x => x.Age, 123)
                .GenerateCommand();

            int executeCommand = _db.Session.Update<Person>(where)
                .Set(x => x.Age, 123)
                .ExecuteCommand();

            int count3 = _db.Session.Query<Person>().Where(endWere).Count();

            Assert.AreEqual(count, count3);
        }

        [Test]
        public void Update_Set_Age_and_LastName_Where_Name_Is_James()
        {
            Expression<Func<Person, bool>> where = p => p.FirstName == "James";
            Expression<Func<Person, bool>> endWere = p => p.FirstName == "James" && p.Age == 123 && p.LastName == "Prime";

            int count = _db.Session.Query<Person>().Where(where).Count();
            int count2 = _db.Session.Query<Person>().Where(endWere).Count();

            string generateCommand = _db.Session.Update<Person>(where)
                .Set(x => x.Age, 123)
                .Set(x=>x.LastName, "Prime")
                .GenerateCommand();

            int executeCommand = _db.Session.Update<Person>(where)
                .Set(x => x.Age, 123)
                .Set(x => x.LastName, "Prime")
                .ExecuteCommand();

            int count3 = _db.Session.Query<Person>().Where(endWere).Count();

            Assert.AreEqual(count, count3);
        }

    }
}


